//
// Created by LiuYou on 2021/5/8.
//

#include <cstring>
#include "CMyString.hpp"

CMyString::CMyString(char* mPData) {
    if (mPData == nullptr) {
        m_PData = new char[1]();
        m_PData[0] = '\0';
    } else {
        m_PData = new char[std::strlen(mPData) + 1]();
        std::memset(m_PData, '\0', std::strlen(mPData) + 1);
        std::strcpy(m_PData, mPData);
    }
}

CMyString::CMyString(const CMyString& str) {
    m_PData = new char[std::strlen(str.getMPData()) + 1]();
    std::memset(m_PData, '\0', std::strlen(str.getMPData()) + 1);
    std::strcpy(m_PData, str.getMPData());
}

CMyString::~CMyString() {
    delete[] m_PData;
}

CMyString&
CMyString::operator=(const CMyString& rhs) {
    if (this == &rhs) {
        return *this;
    } else {
        delete[] m_PData;
        m_PData = new char[std::strlen(rhs.m_PData) + 1]();
        std::memset(m_PData, '\0', std::strlen(rhs.m_PData) + 1);
        std::strcpy(m_PData, rhs.m_PData);
        return *this;
    }
}

char* CMyString::getMPData() const {
    return m_PData;
}
